import pygame, sys
from pygame.locals import *

# set up pygame
pygame.init()

S_WIDTH = 640
S_HEIGHT = 480

# set up the window
windowSurface = pygame.display.set_mode((S_WIDTH, S_HEIGHT), 0, 32)
pygame.display.set_caption('ch2_6')

BLACK = (0, 0, 0)

bg = pygame.Rect(0, 0, S_WIDTH, S_HEIGHT)
bgImage = pygame.image.load('bg.bmp')

MAP_W = 64
MAP_H = 32
MAP_L = 32 * 9
MAP_T = 0

mapRect = pygame.Rect(MAP_L, MAP_T, MAP_W, MAP_H)

map0 = pygame.image.load('map0.bmp')
map0.set_colorkey(BLACK)
map1 = pygame.image.load('map1.bmp')
map1.set_colorkey(BLACK)
map2 = pygame.image.load('map2.bmp')
map2.set_colorkey(BLACK)
map3 = pygame.image.load('map3.bmp')
map3.set_colorkey(BLACK)
maps = [map0, map1, map2, map3]

map_array = [ [2,2,2,2,2,0,1,0,1,0],
              [3,3,2,2,0,0,0,1,1,0],
              [3,0,0,0,0,0,0,0,1,2],
              [2,2,0,0,0,0,0,2,2,2],
              [2,2,0,0,0,0,2,2,2,2],
              [2,2,0,0,0,2,2,0,0,2],
              [2,0,0,2,2,2,0,0,1,0],
              [0,0,2,0,0,0,1,1,1,1],
              [0,2,0,3,3,3,3,3,3,1],
              [2,0,3,3,3,3,3,3,3,3] ]

SCENE_W = 50
SCENE_H = 60
sceneRect = pygame.Rect(MAP_L + (MAP_W - SCENE_W)/2, MAP_T - (SCENE_H - MAP_H / 2), SCENE_W, SCENE_H)
tree = pygame.image.load('scene1.bmp')
tree.set_colorkey(BLACK)
house = pygame.image.load('scene2.bmp')
house.set_colorkey(BLACK)
scenes = [tree, house]

scene_array = [ [0,2,2,0,2,0,1,0,1,1],
               [0,0,0,0,0,0,0,1,1,0],
               [0,0,0,0,0,0,1,0,1,0],
               [0,0,1,0,1,0,0,0,2,0],
               [2,2,0,0,1,0,0,0,0,2],
               [0,0,0,0,0,0,0,0,0,0],
               [0,0,1,0,0,0,0,0,1,0],
               [0,0,0,0,0,0,1,1,1,1],
               [1,0,0,0,0,0,0,0,0,1],
               [2,0,0,0,0,0,0,0,0,0] ]

windowSurface.fill(BLACK)
windowSurface.blit(bgImage, bg)

for ridx, r in enumerate(map_array):
    for cidx, c in enumerate(r):
        rect = mapRect.move( (cidx - ridx) * MAP_W / 2, (ridx + cidx) * MAP_H / 2)
        windowSurface.blit(maps[c], rect, (0, 0, MAP_W, MAP_H))
        svalue = scene_array[ridx][cidx]
        if svalue == 0:
            continue
        elif svalue == 1:
            rect = sceneRect.move((cidx - ridx) * MAP_W / 2, (ridx + cidx) * MAP_H / 2)
            windowSurface.blit(scenes[svalue - 1], rect, (0, 0, SCENE_W, SCENE_H))            
        elif svalue == 2:
            rect = sceneRect.move((cidx - ridx) * MAP_W / 2, (ridx + cidx) * MAP_H / 2 + 10)
            windowSurface.blit(scenes[svalue - 1], rect, (0, 0, SCENE_W, SCENE_H))     

pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
